package com.qg612324.account.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name = "users")
@UniqueConstraint(columnNames = { "user_name" })
public class UserModel implements UserDetails {

	/**
     */
	private static final long serialVersionUID = 9210147740309111349L;
	private Long id;
	private String username;

	private Date createDate;
	private boolean isLocked;
	private Set<Role> roles;
	private Integer version;

	

	/**
	 * Default constructor for Hibernate.
	 */
	public UserModel() {
	}

	/**
	 * 
	 * @param id
	 *            user ID
	 * @param name
	 *            user name
	 */
	public UserModel(Long id, String name) {
		this.id = id;
		this.username = name;
	}

	@Id
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Column(columnDefinition = "boolean default false")
	public boolean isLocked() {
		return isLocked;
	}

	public void setLocked(boolean isLocked) {
		this.isLocked = isLocked;
	}


	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Override
	@Transient
	public Collection<GrantedAuthority> getAuthorities() {
		if (getRoles() == null) {
			return Collections.emptyList();
		}
		return new ArrayList<GrantedAuthority>(getRoles());
	}

	@Override
	@Transient
	public String getPassword() {

		return null;
	}

	@ManyToMany(fetch = FetchType.EAGER)
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@Override
	@Transient
	public boolean isAccountNonExpired() {

		return true;
	}

	@Transient
	public Object getSalt() {
		return this.getCreateDate().getTime();
	}

	@Override
	@Transient
	public boolean isAccountNonLocked() {

		return true;
	}

	@Override
	@Transient
	public boolean isCredentialsNonExpired() {

		return true;
	}

	@Override
	@Transient
	public boolean isEnabled() {

		return true;
	}

	@Version
	@Column(name = "OPTLOCK", columnDefinition = "int default 0")
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

}
